//
//  MBProgressHUD+PYAdd.m
//  IHome
//
//   Created by zhujizheng on 2020/2/18.
//  Copyright © 2019年 com.zhujizheng. All rights reserved.
//

#import "MBProgressHUD+PYAdd.h"

@implementation MBProgressHUD (PYAdd)

+ (void)py_showIndicatorWithText:(NSString *)text forView:(nullable UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    [self wj_showActivityLoading:text toView:view];
}

+ (void)py_showText:(NSString *)text forView:(nullable UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    [self wj_showPlainText:text hideAfterDelay:2 view:view];
}

+ (void)py_showText:(NSString *)text
{
    if (!text) {
        return;
    }
    [self py_showText:text forView:nil];
}

#pragma mark - 移除HUD
+ (void)py_hideHUDForView:(nullable UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    [self wj_hideHUDForView:view];
}

+ (void)py_hide
{
    [self py_hideHUDForView:nil];
}

#pragma mark - 加载进度相关
+ (void)py_showProgressHUD:(float)percent text:(NSString *)text forView:(UIView *)view
{
    [self py_showProgressHUD:percent
                        text:text
                     forView:view
                        mode:MBProgressHUDModeDeterminate
            shadowBackground:NO];
}

+ (MBProgressHUD *)py_showProgressHUD:(float)percent
                      text:(NSString *)text
                   forView:(nullable UIView *)view
                      mode:(MBProgressHUDMode)mode
          shadowBackground:(BOOL)shadowBackground
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    MBProgressHUD *hud = nil;
    for (UIView *hudView in view.subviews) {
        if (hudView.tag == 20190416 && [hudView isKindOfClass:[MBProgressHUD class]]) {
            hud = (MBProgressHUD *)hudView;
        }
    }
    if (hud == nil) {
        hud = [MBProgressHUD showHUDAddedTo:view animated:NO];
        if (shadowBackground) {
            hud.backgroundColor = [UIColor clearColor];
            [UIView animateWithDuration:PY_Animation_Duration animations:^{
                hud.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
            }];
        } else {
            hud.backgroundColor = [UIColor clearColor];
        }
        hud.mode = mode;
        hud.labelText = text;
        hud.tag = 20190416;
        // 隐藏时候从父控件中移除
        hud.removeFromSuperViewOnHide = YES;
    }
    hud.progress = percent;
    if (percent == 1) {
        [hud hide:YES];
    }
    return hud;
}

+ (void)py_hideProgressHUDForView:(UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    MBProgressHUD *hud = nil;
    for (UIView *hudView in view.subviews) {
        if (hudView.tag == 20190416 && [hudView isKindOfClass:[MBProgressHUD class]]) {
            hud = (MBProgressHUD *)hudView;
            [hud hide:YES];
        }
    }
}

+ (void)py_showProgressString:(NSString *)progressString forView:(nullable UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    UILabel *label = nil;
    for (UIView *lbl in view.subviews) {
        if (lbl.tag == 20190416 && [lbl isKindOfClass:[UILabel class]]) {
            label = (UILabel *)lbl;
            break;
        }
    }
    if (!label) {
        label = [[UILabel alloc] initWithFrame:view.bounds];
        label.tag = 20190416;
        label.font = PY_Font_Bold(16);
        label.textColor = PY_Medium_Color;
        label.textAlignment = NSTextAlignmentCenter;
        [view addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
    }
    label.text = progressString;
}

+ (void)py_hideProgressStringForView:(nullable UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    UILabel *label = nil;
    for (UIView *lbl in view.subviews) {
        if (lbl.tag == 20190416 && [lbl isKindOfClass:[UILabel class]]) {
            label = (UILabel *)lbl;
            [label mas_remakeConstraints:^(MASConstraintMaker *make) {
                
            }];
            [label removeFromSuperview];
        }
    }
}

+ (void)py_showProgressIndicatorForView:(nullable UIView *)view;
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;

    UIActivityIndicatorView *indicatorView = nil;
    for (UIView *indicator in view.subviews) {
        if (indicator.tag == 20190416 && [indicator isKindOfClass:[UIActivityIndicatorView class]]) {
            indicatorView = (UIActivityIndicatorView *)indicator;
            break;
        }
    }
    if (!indicatorView) {
        indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:view.bounds];
        indicatorView.tag = 20190416;
        indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
        [view addSubview:indicatorView];
        [indicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
    }
    [indicatorView startAnimating];
}

+ (void)py_hideProgressIndicatorForView:(nullable UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;

    UIActivityIndicatorView *indicatorView = nil;
    for (UIView *indicator in view.subviews) {
        if (indicator.tag == 20190416 && [indicator isKindOfClass:[UIActivityIndicatorView class]]) {
            indicatorView = (UIActivityIndicatorView *)indicator;
            [indicatorView mas_remakeConstraints:^(MASConstraintMaker *make) {
                
            }];
            [indicatorView stopAnimating];
            [indicatorView removeFromSuperview];
        }
    }
}

#pragma mark - 成功/失败的图片提示
+ (void)py_showSuccessViewWithText:(NSString *)text forView:(nullable UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;

    [self wj_showSuccess:text hideAfterDelay:1 toView:view];
}

+ (void)py_showFailureViewWithText:(NSString *)text forView:(nullable UIView *)view
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;

    [self wj_showError:text hideAfterDelay:1 toView:view];
}

@end

